<?php

/**
 * @file classes/manager/form/CTKTTHForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class CTKTTHForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal CTKTTHs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class CTKTTHForm extends Form {

	/** @var $CTKTTHId int The ID of the CTKTTH being edited */
	var $CTKTTHId;

	/** @var $includeCTKTTHEditor object Additional CTKTTH editor to
	 *       include in assigned list for this CTKTTH
	 */
	var $includeCTKTTHEditor;

	/** @var $omitCTKTTHEditor object Assigned CTKTTH editor to omit from
	 *       assigned list for this CTKTTH
	 */
	var $omitCTKTTHEditor;

	/** @var $CTKTTHEditors array List of user objects representing the
	 *       available CTKTTH editors for this journal.
	 */
	var $CTKTTHEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function CTKTTHForm($CTKTTHId = null) {

		parent::Form('acctCN/CTKTTHForm.tpl');

		$journal =& Request::getJournal();
		$this->CTKTTHId = $CTKTTHId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'tentk', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
		

		// Get a list of CTKTTH editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->CTKTTHEditors =& $roleDao->getUsersByRoleId(ROLE_ID_TMTG_EDITOR, $journal->getId());
		$this->CTKTTHEditors =& $this->CTKTTHEditors->toArray();

		
	}

	/**
	 * When displaying the form, include the specified CTKTTH editor
	 * in the assigned list for this CTKTTH.
	 * @param $CTKTTHEditorId int
	 */
	function includeCTKTTHEditor($CTKTTHEditorId) {
		foreach ($this->CTKTTHEditors as $key => $junk) {
			if ($this->CTKTTHEditors[$key]->getId() == $CTKTTHEditorId) {
				$this->includeCTKTTHEditor =& $this->CTKTTHEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified CTKTTH editor from
	 * the assigned list for this CTKTTH.
	 */
	function omitCTKTTHEditor($CTKTTHEditorId) {
		foreach ($this->CTKTTHEditors as $key => $junk) {
			if ($this->CTKTTHEditors[$key]->getId() == $CTKTTHEditorId) {
				$this->omitCTKTTHEditor =& $this->CTKTTHEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$CTKTTHDao =& DAORegistry::getDAO('CTKTTHDAO');
		return $CTKTTHDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display() {
		$journal =& Request::getJournal();
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->CTKTTHId)) {
			$CTKTTHDao =& DAORegistry::getDAO('CTKTTHDAO');
			$CTKTTH =& $CTKTTHDao->getCTKTTH($this->CTKTTHId, false, false);
			if ($CTKTTH == null) {
				unset($this->CTKTTHId);
			} else {
				$this->_data = array(
					'matk' => $CTKTTH->getmatk(), // Localized
					'tentk' => $CTKTTH->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'tentk'));
	}

	/**
	 * Save CTKTTH.
	 */
	function execute() {

		$CTKTTHDao =& DAORegistry::getDAO('CTKTTHDAO');

		if (isset($this->CTKTTHId)) {
			$CTKTTH =& $CTKTTHDao->getCTKTTH($this->CTKTTHId, false, false);
		}

		if (!isset($CTKTTH)) {
			$CTKTTH = new CTKTTH();
		}

		$CTKTTH->setmatk($this->getData('matk'), null); // Localized
		$CTKTTH->settentk($this->getData('tentk'), null); // Localized

		
		if (isset($this->CTKTTHId)) {
			$CTKTTHDao->updateCTKTTH($CTKTTH);
			$CTKTTHId = $CTKTTH->getId();

		} else {

			$CTKTTHId = $CTKTTHDao->insertCTKTTH($CTKTTH);
			
		}

	}
}

?>
